<?php
/**
 * Created by PhpStorm.
 * User: fang_
 * Date: 2016/9/29
 * Time: 18:12
 */
// 根据用户指定的文件下载位置，自动解压并重命名文件夹
$path = 'g:/abc';
if ($handle = opendir($path)) {
    echo "Directory handle: $handle\n";
    echo "Files:\n";

    /* 这是正确地遍历目录方法 */
    while (false !== ($file_name = readdir($handle))) {
        if (!(stripos($file_name,'.rar') === false)) // rar 文件解压
        {
            $rar_file = rar_open($path.$file_name);
            $list = rar_list($rar_file);
            foreach($list as $file) {
                $entry = rar_entry_get($rar_file, $file);
                mkdir($path.'/'.$file_name);
                $entry->extract("./".$file_name); // extract to the current dir
            }
            rar_close($rar_file);
        }
        echo "$file\n";
    }
}